"""hw1.py  ejf Oct 17,2013 for mwe
 Read  hw0.txt, whose lines were created with:
 out = "%s:%s:%s,%s" %(page,hw,l1,l2)

 'Normalize' all headword spellings.
 This accomplished by removing numbers (anglicized sanskrit)
 Then output the same format, using the normalized headword
 Here are the normalizations:
 ()
"""
import re
import sys
def hw_normalize(hw):
 hw0 = hw
  #hw = re.sub(r'\xa4','',hw)
 #hw = re.sub(r' ','',hw)
 #hw = re.sub(r'\{\?\?\}','',hw)
 #hw = re.sub(r'[.].*$','',hw)
 #hw = re.sub(r'\(s\)','',hw)

 #hw = re.sub(r'^\(Sarva\) ','',hw)
 #hw = re.sub(r' \(.*$','',hw)
 #hw = re.sub(r'\(.\)','',hw)
 #hw = re.sub(r'\([a-zA-Z0-9]*\)','',hw)
 #hw = re.sub(r'\(.*$','',hw)
 #hw = re.sub(r' .*$','',hw)
 #hw = hw.lower() # make lower case
 #hw = re.sub(r"[^a-z0-9]$","",hw)
 hw = re.sub(r"[0-9]","",hw);
 m = re.search(r"([^a-z '-])",hw)
 if m:
  c = m.group(1)
  cint = ord(c)
  print "headword '%s' (%s) has unexpected character '%s' = %s" %(hw,hw0,c,cint)
 return hw

filename=sys.argv[1] 
fileout =sys.argv[2] 
filenote =sys.argv[3]
f = open(filename,'r')
fout = open(fileout,'w')
fnote = open(filenote,'w')
n = 0
nnote = 0
nout = 0 # number of headword lines written to output
for line in f:
 n = n+1
 line = line.strip() # remove starting or ending whitespace
 (pagecol,hw0,line12) = re.split(':',line)

 hw = hw_normalize(hw0)
 out = "%s:%s:%s" %(pagecol,hw,line12)
 fout.write("%s\n" % out);
 nout = nout + 1
 if ((hw != hw0) and ((hw+"-")!=hw0)):
  out = "%s:  '%s' => '%s'  :%s" %(pagecol,hw0,hw,line12)
  fnote.write("%s\n" % out);
  nnote = nnote + 1
f.close()
fout.close()
fnote.close()
print "file %s has %s lines" % (filename,n)
print "%s headwords written to file %s" % (nout,fileout)
print "%s headwords with normalization changes written to %s" % (nnote,filenote)
